package com.sharing.util;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @author MingTian
 * @version 1.0
 * @description TODO ES 客户端对象的工具栏类
 * @date 2023/5/12 9:34
 */
public class EsClient {
    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

//        // 创建索引 - 请求对象
//        CreateIndexRequest request = new CreateIndexRequest("user");
//        // 发送请求，获取响应
//        CreateIndexResponse response = client.indices().create(request,
//                RequestOptions.DEFAULT);
//        boolean acknowledged = response.isAcknowledged();
//        // 响应状态
//        System.out.println("操作状态 = " + acknowledged);


//        // 查询索引 - 请求对象
//        GetIndexRequest request = new GetIndexRequest("user");
//        // 发送请求，获取响应
//        GetIndexResponse response = client.indices().get(request,
//                RequestOptions.DEFAULT);
//        System.out.println("aliases:"+response.getAliases());
//        System.out.println("mappings:"+response.getMappings());
//        System.out.println("settings:"+response.getSettings());

        // 删除索引 - 请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        // 发送请求，获取响应
        AcknowledgedResponse response = client.indices().delete(request,
                RequestOptions.DEFAULT);
        // 操作结果
        System.out.println("操作结果 ： " + response.isAcknowledged());


        // 关闭客户端连接
        client.close();
    }
}
